from langchain.chains import LLMChain
from langchain_community.chat_models import ChatOpenAI
from langchain_core.prompts import PromptTemplate

llm = ChatOpenAI(temperature=0.7, max_tokens=1000, model_name='gpt-3.5-turbo')

template = """你是一个翻译家，需要先识别一下src的语种，然后将result的内容翻译为src的语种。

src: {src}
result: {result}："""
translate_prompt_template = PromptTemplate(input_variables=["src", "result"], template=template)
translate_chain = LLMChain(llm=llm, prompt=translate_prompt_template, verbose=True)


def parse_translation(translation: str):
    lines = translation.split('\n')

    result: list[str] = []
    index = 0
    for line in lines:
        if line.find('result: ') != -1:
            result = lines[index:]
        index = index + 1
    result[0] = result[0].replace('result: ', '')
    parsed_str = str.join('\n', result)
    print(parsed_str)
    return parsed_str


if __name__ == '__main__':
    parse_translation('''根据您提供的信息，src的语种是英语。现在我将把result的内容翻译成英语：

result: Nantong University''')
